package com.hivekion.common.security;

import com.hivekion.system.domain.SysUser;
import com.hivekion.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;

@Component
public class AuthenticationSuccessListener implements ApplicationListener<AuthenticationSuccessEvent> {

    @Autowired
    private ISysUserService userService;

    @Override
    public void onApplicationEvent(AuthenticationSuccessEvent authenticationSuccessEvent) {
        String userName = authenticationSuccessEvent.getAuthentication().getPrincipal().toString();
        SysUser user = userService.getUserByUserName(userName);
        if (user != null) {
            user.setStatus(0);
            user.setFails(0);
            userService.saveOrUpdate(user);
        }
    }
}
